package com.hddz.acquisition.activity.stock;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hddz.acquisition.AcquistionApp;
import com.hddz.acquisition.AppSetting;
import com.hddz.acquisition.R;
import com.hddz.acquisition.SmartCallBack;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.model.ArmshaftItem;
import com.hddz.acquisition.model.BaseListItem;
import com.hddz.acquisition.model.Command;
import com.hddz.acquisition.model.IListItem;
import com.hddz.acquisition.model.JsonRespose;
import com.hddz.acquisition.view.ArmshaftAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_stock_armshaft_list)
public class ArmshaftListActivity extends BaseActivity {
    @ViewInject(R.id.list)
    private ListView list;
    ArmshaftAdapter adapter = null;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_armshaft_list);
        adapter = new ArmshaftAdapter(this);
        list.setAdapter(adapter);
        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           final int position, long id) {
                // TODO Auto-generated method stub
                AlertDialog.Builder ibuilder = new AlertDialog.Builder(
                        ArmshaftListActivity.this);
                ibuilder.setTitle("提示");
                ibuilder.setMessage("是否删除此条记录？");
                ibuilder.setPositiveButton(R.string.remove,
                        new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub

                                IListItem item = adapter.getItem(position);
                                sendCommand("0908", ((BaseListItem) item).cCode);
                                dialog.dismiss();
                            }
                        });
                ibuilder.setNegativeButton(R.string.cancel, null);
                ibuilder.create().show();

                return false;
            }
        });
        sendCommand("0909", GUID);
    }

    private void sendCommand(String cmd, String guid) {
        baseSendCommand(cmd, "", "", "", "", "", "", guid, new SmartCallBack<JsonRespose<ArmshaftItem>>() {
            @Override
            public void onSuccess(JsonRespose<ArmshaftItem> result) {
                super.onSuccess(result);


                if (result.code > 0) {
                    if (cmd.equals("0909") && result.List != null) {
                        adapter.updateList(result.List);
                    } else if (cmd.equals("0908")) {
                        adapter.removeItem(GUID);
                    }
                }
            }
        });
    }


    @Override
    public void addResponse(JsonRespose json) {

    }

    @Override
    public void addErr(String err) {

    }

    @Override
    public void receiveCode(String code) {

    }

    @Override
    public void backDo() {
        onBack(null);
    }

    @Override
    public void finishDo() {

    }

    @Event(R.id.btn_back)
    private void onBack(View view) {
            finish();
    }
}
